package com.ruoyi.api.user;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.api.user.requestVo.MachineBindRequestVo;
import com.ruoyi.api.user.requestVo.MachineDismissRequestVo;
import com.ruoyi.api.user.requestVo.MachineInfoRequestVo;
import com.ruoyi.api.user.requestVo.MachineModifyRequestVo;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.user.domain.Machine;
import com.ruoyi.user.domain.User;
import com.ruoyi.user.domain.UserMachine;
import com.ruoyi.user.service.IMachineService;
import com.ruoyi.user.service.IUserMachineService;

import io.swagger.annotations.ApiOperation;

/**
 * 设备 信息操作处理
 * 
 * @author ruoyi
 * @date 2018-12-23
 */
@Controller
@RequestMapping("/api/machine")
public class ApiMachineController extends BaseController
{
    private String prefix = "machine";
	
	@Autowired
	private IMachineService machineService;
	
	@Autowired
	private IUserMachineService userMachineService;
	
	/**
	 * 设备详情
	 */
	@ResponseBody
	@PostMapping("/machineInfo")
	@ApiOperation(value="根据ID查询设备详情", notes="根据ID查询设备详情",produces = "application/json")
	public HashMap<String, Object> machineInfo(@RequestBody MachineInfoRequestVo infoRequestVo){
		Machine m = machineService.selectMachineById(infoRequestVo.getId());
		/*String result = JSONUtil.parseEntityToJson(m);*/
		return successApp(m);
	}
	
	/**
	 * 设备信息修改
	 */
	@ResponseBody
	@PostMapping("/machineModify")
	@ApiOperation(value="设备信息修改", notes="设备信息修改",produces = "application/json")
	public HashMap<String, Object> machineModify(@RequestBody MachineModifyRequestVo userVo){
		Machine userOld = machineService.selectMachineById(userVo.getId());
		if(userVo.getNickName()!=null){
			userOld.setNickName(userVo.getNickName());
		}
		if(userVo.getPhone()!=null){
			userOld.setPhone(userVo.getPhone());
		}
		if(userVo.getSex()!=null){
			userOld.setSex(userVo.getSex());
		}
		if(userVo.getBirthday()!=null){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			try {
				userOld.setBirthday(sdf.parse(userVo.getBirthday()));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(userVo.getBloodVal()!=null){
			userOld.setBloodVal(userVo.getBloodVal());
		}
		if(userVo.getStepGoal()!=null){
			userOld.setStepGoal(userVo.getStepGoal());
		}
		if(userVo.getSleepGoal()!=null){
			userOld.setSleepGoal(userVo.getSleepGoal());
		}
		if(userVo.getHeight()!=null){
			userOld.setHeight(userVo.getHeight());
		}
		if(userVo.getWeight()!=null){
			userOld.setWeight(userVo.getWeight());
		}
		if(userVo.getBloodType()!=null){
			userOld.setBloodType(userVo.getBloodType());
		}
		if(userVo.getMedicalHistory()!=null){
			userOld.setMedicalHistory(userVo.getMedicalHistory());
		}
		userOld.setUpdateTime(new Date());
		machineService.updateMachine(userOld);
		return successApp("");
	}
	
	/**
	 * 设备绑定
	 */
	@ResponseBody
	@PostMapping("/machineBind")
	@ApiOperation(value="设备绑定", notes="设备绑定",produces = "application/json")
	public HashMap<String, Object> machineBind(HttpServletRequest request,@RequestBody MachineBindRequestVo bindRequestVo){
		Machine param = new Machine();
		param.setCode(bindRequestVo.getCode());
		User user = (User) request.getAttribute("user");
		List<Machine> list = machineService.selectMachineList(param);
		boolean flag = true;
		if(list!=null&&list.size()>0){//设备已存在
			Machine m = list.get(0);
			UserMachine paramUM = new UserMachine();
			paramUM.setMMachine(m.getId());
			paramUM.setUId(user.getId());
			paramUM.setStatus(1);
			List<UserMachine> listUM = userMachineService.selectUserMachineList(paramUM);
			if(listUM!=null&&listUM.size()>0){//已绑定
				flag = false;
				return errorApp("mc-0001","该设备已绑定");
			}else{
				UserMachine newUM = new UserMachine();
				newUM.setCreateTime(new Date());
				newUM.setMCode(bindRequestVo.getCode());
				newUM.setMMachine(m.getId());
				newUM.setStatus(1);
				newUM.setUId(user.getId());
				userMachineService.insertUserMachine(newUM);
			}
			m.setSource(bindRequestVo.getSource());
			machineService.updateMachine(m);
		}else{
			Machine newM = new Machine();
			newM.setCode(bindRequestVo.getCode());
			newM.setName(bindRequestVo.getCode());
			newM.setSource(bindRequestVo.getSource());
			newM.setCreateTime(new Date());
			machineService.insertMachine(newM);
			Machine newMP = new Machine();
			newMP.setCode(bindRequestVo.getCode());
			newMP.setName(bindRequestVo.getCode());
			newMP.setSource(bindRequestVo.getSource());
			List<Machine> list1 = machineService.selectMachineList(newMP);
			if(list1!=null&&list1.size()>0){
				UserMachine newUM = new UserMachine();
				newUM.setCreateTime(new Date());
				newUM.setMCode(bindRequestVo.getCode());
				newUM.setMMachine(list1.get(0).getId());
				newUM.setStatus(1);
				newUM.setUId(user.getId());
				userMachineService.insertUserMachine(newUM);
			}else{
				return errorApp("mc-0002","绑定出错");
			}
		}
		JSONObject param1 = new JSONObject();
		param1.put("imei", bindRequestVo.getCode());
		param1.put("mode", "1");
		HttpUtils.sendPostJson("http://api.jiai.pro:8080/jiai/positionMode", param1.toJSONString(),bindRequestVo.getCode());
		return successApp("");
	}
	
	/**
	 * 设备解绑
	 */
	@ResponseBody
	@PostMapping("/machineDismiss")
	@ApiOperation(value="设备解绑", notes="设备解绑",produces = "application/json")
	public HashMap<String, Object> machineDismiss(HttpServletRequest request,@RequestBody MachineDismissRequestVo vo){
		UserMachine paramUM = new UserMachine();
		User user = (User) request.getAttribute("user");
		paramUM.setUId(user.getId());
		paramUM.setStatus(1);
		paramUM.setMMachine(vo.getId());
		List<UserMachine> list = userMachineService.selectUserMachineList(paramUM);
		if(list!=null&&list.size()>0){
			UserMachine machine = list.get(0);
			machine.setStatus(0);
			userMachineService.updateUserMachine(machine);
		}
		return successApp("");
	}
	
	/**
	 * 设备绑定列表
	 */
	@ResponseBody
	@PostMapping("/machineBindList")
	@ApiOperation(value="设备绑定列表", notes="设备绑定列表",produces = "application/json")
	public HashMap<String, Object> machineBindList(HttpServletRequest request){
		User user = (User) request.getAttribute("user");
		UserMachine paramUM = new UserMachine();
		paramUM.setUId(user.getId());
		paramUM.setStatus(1);
		List<UserMachine> listUM = userMachineService.selectUserMachineList(paramUM);
		List<Machine> listM = new ArrayList<Machine>();
		for(UserMachine um:listUM){
			Machine mc = machineService.selectMachineById(um.getMMachine());
			if(mc!=null){
				listM.add(mc);
			}
		}
		return successApp(listM);
	}
	
	
}
